package guda.house.web.action.stat;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.BillPayBiz;
import guda.house.biz.BillStatBiz;
import guda.house.biz.BillStatOtherBiz;
import guda.house.biz.beans.vo.AccountEstateXlsVO;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.beans.vo.TypeVO;
import guda.house.biz.beans.vo.VillageVO;
import guda.house.biz.enums.StatTypeEnum;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.form.bill.AccountEstateSearchForm;
import guda.house.biz.form.stat.StatFeeForm;
import guda.house.biz.query.AccountEstateQuery;
import guda.house.biz.query.BuildingQuery;
import guda.house.biz.query.OfficeEmployeeQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.biz.security.AppUtil;
import guda.house.biz.util.DateUtil;
import guda.house.biz.util.ExportExcelUtil;
import guda.house.biz.util.PriceUtil;
import guda.house.dal.dao.BuildingDAO;
import guda.house.dal.dao.OfficeEmployeeDAO;
import guda.house.dal.dao.SysVarDAO;
import guda.house.dal.dataobject.*;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by well on 15/11/28.
 */
@Controller
public class StatAction {

    @Autowired
    private BillStatBiz billStatBiz;
    @Autowired
    private SysVarDAO sysVarDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private OfficeEmployeeDAO officeEmployeeDAO;
    @Autowired
    private BillStatOtherBiz billStatOtherBiz;



    @RequestMapping(value = "stat/stat.htm", method = RequestMethod.GET)
    public String stat(HttpServletRequest request, ModelMap modelMap, @Valid StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            return "stat/stat.vm";
        }

        BizResult stat = billStatBiz.stat(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/stat.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }





    @RequestMapping(value = "stat/tieOff.htm", method = RequestMethod.GET)
    public String tieOff(HttpServletRequest request, ModelMap modelMap, @Valid StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            return "stat/tieOff.vm";
        }
        BizResult stat = billStatBiz.statTieOff(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/tieOff.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }



    @RequestMapping(value = "stat/elecRate.htm", method = RequestMethod.GET)
    public String elecRate(HttpServletRequest request, ModelMap modelMap, StatFeeForm statFeeForm, BindingResult result) {
        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/elecRate.vm";
        }
        BizResult stat = billStatBiz.statElec(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/elecRate.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/waterRate.htm", method = RequestMethod.GET)
    public String waterRate(HttpServletRequest request, ModelMap modelMap, StatFeeForm statFeeForm, BindingResult result) {
        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/waterRate.vm";
        }
        BizResult stat = billStatBiz.statWater(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/waterRate.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/waterElecRate.htm", method = RequestMethod.GET)
    public String waterElecRate(HttpServletRequest request, ModelMap modelMap, @Valid StatFeeForm statFeeForm, BindingResult result) {
        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/waterElecRate.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getStartDate() )){
            result.reject("startDate","不能为空");
            return "stat/waterElecRate.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getEndDate() )){
            result.reject("endDate","不能为空");
            return "stat/waterElecRate.vm";
        }
        BizResult stat = billStatBiz.statWaterElec(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/waterElecRate.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }


    @RequestMapping(value = "stat/parkingRate.htm", method = RequestMethod.GET)
    public String parkingRate(HttpServletRequest request, ModelMap modelMap, StatFeeForm statFeeForm, BindingResult result) {
        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/parkingRate.vm";
        }
        BizResult stat = billStatBiz.statParking(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/parkingRate.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/estateRate.htm", method = RequestMethod.GET)
    public String estateRate(HttpServletRequest request, ModelMap modelMap,@Valid StatFeeForm statFeeForm, BindingResult result) {
        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/estateRate.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getStartDate() )){
            result.reject("startDate","不能为空");
            return "stat/estateRate.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getEndDate() )){
            result.reject("endDate","不能为空");
            return "stat/estateRate.vm";
        }
        BizResult stat = billStatBiz.statEstate(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/estateRate.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }


    @RequestMapping(value = "stat/estateRateLastYear.htm", method = RequestMethod.GET)
    public String estateRateLastYear(HttpServletRequest request, ModelMap modelMap, @Valid StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/estateRateLastYear.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getStartDate() )){
            result.reject("startDate","不能为空");
            return "stat/estateRateLastYear.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getEndDate() )){
            result.reject("endDate","不能为空");
            return "stat/estateRateLastYear.vm";
        }
        BizResult stat = billStatBiz.statEstateLastYear(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/estateRateLastYear.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/statCharge.htm", method = RequestMethod.GET)
    public String statCharge(HttpServletRequest request, ModelMap modelMap, @Valid StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/statCharge.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getStartDate() )){
            result.reject("startDate","不能为空");
            return "stat/statCharge.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getEndDate() )){
            result.reject("endDate","不能为空");
            return "stat/statCharge.vm";
        }
        BizResult stat = billStatBiz.statCharge(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/statCharge.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/statDelivery.htm", method = RequestMethod.GET)
    public String statDelivery(HttpServletRequest request, ModelMap modelMap, StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/statDelivery.vm";
        }
        BizResult stat = billStatBiz.statDelivery(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/statDelivery.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/estateRateCompare.htm", method = RequestMethod.GET)
    public String estateRateCompare(HttpServletRequest request, ModelMap modelMap,@Valid  StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/estateRateCompare.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getStartDate() )){
            result.reject("startDate","不能为空");
            return "stat/estateRateCompare.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getEndDate() )){
            result.reject("endDate","不能为空");
            return "stat/estateRateCompare.vm";
        }
        BizResult stat = billStatBiz.estateRateCompare(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/estateRateCompare.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/statInvoice.htm", method = RequestMethod.GET)
    public String statInvoice(HttpServletRequest request, ModelMap modelMap,@Valid  StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/statInvoice.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getStartDate() )){
            result.reject("startDate","不能为空");
            return "stat/statInvoice.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getEndDate() )){
            result.reject("endDate","不能为空");
            return "stat/statInvoice.vm";
        }
        BizResult stat = billStatOtherBiz.statInvoice(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/statInvoice.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }

    @RequestMapping(value = "stat/statEstateDiscount.htm", method = RequestMethod.GET)
    public String statEstateDiscount(HttpServletRequest request, ModelMap modelMap, @Valid StatFeeForm statFeeForm, BindingResult result) {

        if(statFeeForm.getVillageId() == null){
            result.reject("villageId","不能为空");
            return "stat/statEstateDiscount.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getStartDate() )){
            result.reject("startDate","不能为空");
            return "stat/statEstateDiscount.vm";
        }
        if(StringUtils.isEmpty(statFeeForm.getEndDate() )){
            result.reject("endDate","不能为空");
            return "stat/statEstateDiscount.vm";
        }
        BizResult stat = billStatOtherBiz.statInvoice(statFeeForm);
        if (stat.success) {
            modelMap.putAll(stat.data);
            return "stat/statEstateDiscount.vm";
        } else {
            return "redirect:/error.htm?code=" + stat.code;
        }
    }







    @RequestMapping(value = "common/stat/getEstateType.htm", method = RequestMethod.POST)
    public void getEstateType(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {

        long villageId = RequestUtil.getLong(request, "villageId");
        JsonResult result = JsonResult.getInstance();

        SysVarQuery sysVarQuery = new SysVarQuery();
        sysVarQuery.setVillageId(villageId);
        sysVarQuery.setVarType(SysVarTypeEnum.building.name());
        sysVarQuery.setPageSize(500);
        List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
        result.getMap().put("sysVarList", sysVarDOs);
        ResponseHelper.ajaxReturn(result.getMap(), response);

    }



    @RequestMapping(value = "common/stat/getSubType.htm", method = RequestMethod.POST)
    public void getSubType(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {

        String type = request.getParameter("type");
        long villageId = RequestUtil.getLong(request, "villageId");
        JsonResult result = JsonResult.getInstance();
        if(type == null){
            result.setErrMsg("缺少参数");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        if(type.equals(StatTypeEnum.building.name())){
            BuildingQuery buildingQuery = new BuildingQuery();
            buildingQuery.setVillageId(villageId);
            buildingQuery.setPageSize(500);
            List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
            List<TypeVO> typeVOList = new ArrayList<TypeVO>(buildingDOs.size());
            for(BuildingDO buildingDO:buildingDOs){
                TypeVO typeVO = new TypeVO();
                typeVO.setId(buildingDO.getBuildingId());
                typeVO.setName(buildingDO.getBuildingName());
                typeVOList.add(typeVO);
            }
            result.getMap().put("typeVOList", typeVOList);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;

        }else if(type.equals(StatTypeEnum.manage.name())){
            OfficeEmployeeQuery officeEmployeeQuery = new OfficeEmployeeQuery();
            officeEmployeeQuery.setVillageId(villageId);
            officeEmployeeQuery.setPageSize(100);
            List<OfficeEmployeeDO> officeEmployeeDOList = officeEmployeeDAO.selectForPage(officeEmployeeQuery);
            List<TypeVO> typeVOList = new ArrayList<TypeVO>(officeEmployeeDOList.size());
            for(OfficeEmployeeDO officeEmployeeDO:officeEmployeeDOList){
                TypeVO typeVO = new TypeVO();
                typeVO.setId(officeEmployeeDO.getEmployeeId());
                typeVO.setName(officeEmployeeDO.getUserName());
                typeVOList.add(typeVO);
            }
            result.getMap().put("typeVOList", typeVOList);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }else if(type.equals(StatTypeEnum.office.name())){
            SysVarQuery sysVarQuery = new SysVarQuery();
            sysVarQuery.setVillageId(villageId);
            sysVarQuery.setVarType(SysVarTypeEnum.office.name());
            sysVarQuery.setPageSize(500);
            List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
            List<TypeVO> typeVOList = new ArrayList<TypeVO>(sysVarDOs.size());
            for(SysVarDO sysVarDO:sysVarDOs){
                TypeVO typeVO = new TypeVO();
                typeVO.setId(sysVarDO.getVarId());
                typeVO.setName(sysVarDO.getVarValue());
                typeVOList.add(typeVO);
            }
            result.getMap().put("typeVOList", typeVOList);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }else if(type.equals(StatTypeEnum.type.name())){
            SysVarQuery sysVarQuery = new SysVarQuery();
            sysVarQuery.setVillageId(villageId);
            sysVarQuery.setVarType(SysVarTypeEnum.building.name());
            sysVarQuery.setPageSize(500);
            List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
            List<TypeVO> typeVOList = new ArrayList<TypeVO>(sysVarDOs.size());
            for(SysVarDO sysVarDO:sysVarDOs){
                TypeVO typeVO = new TypeVO();
                typeVO.setId(sysVarDO.getVarId());
                typeVO.setName(sysVarDO.getVarValue());
                typeVOList.add(typeVO);
            }
            result.getMap().put("typeVOList", typeVOList);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }else if(type.equals(StatTypeEnum.village.name())){
//            List<VillageVO> villageList = AppUtil.getVillageList();
//            List<TypeVO> typeVOList = new ArrayList<TypeVO>(villageList.size());
//            for(VillageVO villageVO:villageList){
//                TypeVO typeVO = new TypeVO();
//                typeVO.setId(villageVO.getVillageId());
//                typeVO.setName(villageVO.getVillageName());
//                typeVOList.add(typeVO);
//            }
            result.getMap().put("typeVOList", Collections.emptyList());
            ResponseHelper.ajaxReturn(result.getMap(), response);

        }




    }


}
